﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace Mammoo.Data
{
    /// <summary>
    /// 
    /// </summary>
    public static class ProcHelper
    {
        /// <summary>
        /// 执行不返回记录集但有返回值的存储过程
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="spName">存储过程名称</param>
        /// <param name="htParams">包含存储过程参数的哈希表</param>
        /// <returns>存储过程中类型为Output的参数名为key,值为value的HashTable;如果没有Output类型的参数则返回一个空的HashTable.</returns>
        public static Hashtable ExecuteNonQuery(string connectionString, string spName, Hashtable htParams)
        {
            if (htParams == null)
            {
                htParams = new Hashtable();
            }

            Hashtable htOutput = new Hashtable();

            SqlParameter[] sqlParams = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName, false);

            SqlHelper.AssignParameterValues(sqlParams, htParams);

            SqlHelper.ExecuteNonQuery(connectionString, CommandType.StoredProcedure, spName, sqlParams);

            foreach (SqlParameter p in sqlParams)
            {
                if (p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Output || p.Direction == ParameterDirection.ReturnValue)
                {
                    htOutput.Add(p.ParameterName, p.Value);
                }
            }

            return htOutput;
        }

        /// <summary>
        /// 执行返回DataTable的存储过程
        /// </summary>
        /// <param name="connectionString">数据库连接字符串</param>
        /// <param name="spName">存储过程名称</param>
        /// <param name="htParams">包含存储过程参数的哈希表</param>
        /// <param name="htOutput">存储过程中类型为Output的参数名为key,值为value的HashTable;如果没有Output类型的参数则返回一个空的HashTable.</param>
        /// <returns>包含记录集的DataTable</returns>
        public static DataTable ExecuteDataTable(string connectionString, string spName, Hashtable htParams, out Hashtable htOutput)
        {
            htOutput = new Hashtable();

            if (htParams == null)
            {
                htParams = new Hashtable();
            }

            SqlParameter[] sqlParams = SqlHelperParameterCache.GetSpParameterSet(connectionString, spName, false);

            SqlHelper.AssignParameterValues(sqlParams, htParams);

            DataSet ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, spName, sqlParams);

            foreach (SqlParameter p in sqlParams)
            {
                if (p.Direction == ParameterDirection.InputOutput || p.Direction == ParameterDirection.Output || p.Direction == ParameterDirection.ReturnValue)
                {
                    htOutput.Add(p.ParameterName, p.Value);
                }
            }

            DataTable dt = ds.Tables[0];
            ds.Dispose();

            return dt;
        }
    }
}
